import React, { useEffect, useState } from 'react';
import CommonPage from '@/components/page/CommonPage';
import { useModel } from 'umi';
import { fetchOption } from '@/services/commCRUD';
import { format } from '@/utils/datetime';
import { renderNumber } from '@/utils/common';
const daoName = 'Zbdf';
const importName = 'Zbdf';
const columns: API.Column[] = [
  {
    title: '部门',
    width: 150,
    sorter: true,
    key: 'DPNM',
    dataIndex: 'DPNM',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '指标名称',
    width: 150,
    sorter: true,
    key: 'ZBMC',
    dataIndex: 'ZBMC',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '打分',
    width: 150,
    sorter: true,
    key: 'DF',
    dataIndex: 'DF',
    dispaly: true,
    filter: true,
    export: true,
  },
];

const controls: API.Control[] = [
  { name: 'DPNM', type: 'text', label: '部门', grid: 12 / 24 },
  { name: 'ZBMC', type: 'text', label: '指标名称', grid: 12 / 24 },
  { name: 'DF', dataType: 'number', label: '打分', grid: 12 / 24 },
];

const controlsImport: API.Control[] = [
  { name: 'DPNM', type: 'text', label: '部门', grid: 12 / 24 },
  { name: 'DF', dataType: 'number', label: '打分', grid: 12 / 24 },
];

const daoName2 = 'Zbzl';
const importName2 = 'Zbzl';
const columns2: API.Column[] = [
  {
    title: '部门',
    width: 110,
    sorter: true,
    key: 'DPNM',
    dataIndex: 'DPNM',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '指标名称',
    width: 150,
    sorter: true,
    key: 'ZBMC',
    dataIndex: 'ZBMC',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '基数',
    width: 110,
    sorter: true,
    key: 'JS',
    dataIndex: 'JS',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '计划增量',
    width: 110,
    sorter: true,
    key: 'JHZL',
    dataIndex: 'JHZL',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '完成量',
    width: 110,
    sorter: true,
    key: 'WCL',
    dataIndex: 'WCL',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '完成增量',
    width: 110,
    sorter: true,
    key: 'WCZL',
    dataIndex: 'WCZL',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '增量完成率',
    width: 110,
    sorter: true,
    key: 'WCLV',
    dataIndex: 'WCLV',
    dispaly: true,
    filter: true,
    export: true,
    render: (WCLV: string) => renderNumber('.2f%', WCLV),
  },
];

const controls2: API.Control[] = [
  { name: 'DPNM', type: 'text', label: '部门', grid: 12 / 24 },
  { name: 'ZBMC', type: 'text', label: '指标名称', grid: 12 / 24 },
  { name: 'JS', dataType: 'number', label: '基数', grid: 12 / 24 },
  { name: 'JHZL', dataType: 'number', label: '计划增量', grid: 12 / 24 },
  { name: 'WCL', dataType: 'number', label: '完成量', grid: 12 / 24 },
  { name: 'WCZL', dataType: 'number', label: '完成增量', grid: 12 / 24 },
  { name: 'WCLV', dataType: 'number', label: '增量完成率', grid: 12 / 24 },
];
const controlsImport2: API.Control[] = [
  { name: 'DPNM', type: 'text', label: '部门', grid: 12 / 24 },
  { name: 'JS', dataType: 'number', label: '基数', grid: 12 / 24 },
  { name: 'JHZL', dataType: 'number', label: '计划增量', grid: 12 / 24 },
  { name: 'WCL', dataType: 'number', label: '完成量', grid: 12 / 24 },
  { name: 'WCZL', dataType: 'number', label: '完成增量', grid: 12 / 24 },
  { name: 'WCLV', dataType: 'number', label: '增量完成率', grid: 12 / 24 },
];
const Zb: React.FC = () => {
  const { user } = useModel('@@initialState', (ret) => {
    return {
      user: ret.initialState?.user,
    };
  });
  const { permTypes } = useModel('sysInfo', (ret) => ({
    permTypes: ret.getCurPermTypes(),
  }));
  const [paras, setParas] = useState<Record<string, any>>({});
  const [datas, setDatas] = useState<Record<string, any>[]>([]);
  let where: string = '';
  if (permTypes.indexOf('4') == -1) {
    where = `"CZR"=${user?.userID}`;
  }
  const year = new Date();
  const dqyear = year.getFullYear();
  const currMonth = new Date().getMonth();
  const currQuarter = Math.floor(currMonth % 3 == 0 ? currMonth / 3 : currMonth / 3 + 1);
  let lastQuarterFirstDay: string;
  if (currQuarter == 1) {
    lastQuarterFirstDay = dqyear + '-03-31';
  } else if (currQuarter == 2) {
    lastQuarterFirstDay = dqyear + '-06-30';
  } else if (currQuarter == 3) {
    lastQuarterFirstDay = dqyear + '-09-30';
  } else {
    lastQuarterFirstDay = dqyear + '-12-31';
  }
  useEffect(() => {
    fetchOption('ShJhcwDcSetJYDW', where, [])
      .then(function (res: Record<string, any>[]) {
        if (res.length > 0) {
          const id = res[0].ZBID;
          setDatas(res);
          setParas({
            ZBID: id,
            DFJD: format(lastQuarterFirstDay, 'yyyy-MM-dd'),
          });
        }
      })
      .catch(function (ex) {
        console.error('错误', ex);
      });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  let daoname: string = '';
  let column: API.Column[] = [];
  let control: API.Control[] = [];
  let title: string = '';
  let importname: string = '';
  let tempname: string = '';
  let controlsimport: API.Control[] = [];
  if (paras.ZBID === undefined) {
    return <></>;
  }
  const idx = datas.findIndex((d) => d.ZBID === paras.ZBID);
  if (datas[idx].ISDF == 1) {
    daoname = daoName;
    column = columns;
    control = controls;
    title = '指标打分';
    importname = importName;
    tempname = '指标打分导入模板';
    controlsimport = controlsImport;
  } else {
    daoname = daoName2;
    column = columns2;
    control = controls2;
    title = '指标增量';
    importname = importName2;
    tempname = '指标增量导入模板';
    controlsimport = controlsImport2;
  }
  return (
    <CommonPage
      daoName={daoname}
      columns={column}
      controls={control}
      title={title}
      importName={importname}
      importParamVals={{
        DFJD: paras.DFJD,
        ZBID: '' + paras.ZBID,
      }}
      importMode="DeleteAfterInsert"
      importParamNames={['DFJD', 'ZBID']}
      importTemplateName={tempname}
      importControls={controlsimport}
      keyColName="ID"
      onShortcutChange={async (vals) => setParas(vals)}
      shortcutFilters={{
        controls: [
          {
            name: 'DFJD',
            dataType: 'date',
            type: 'quarter',
            label: '选择季度',
            rules: [{ required: true, message: '季度不能为空' }],
            width: 120,
            initValue: paras.DFJD,
          },
          {
            name: 'ZBID',
            label: '选择指标',
            type: 'select',
            width: 260,
            rules: [{ required: true, message: '指标不能为空' }],
            dataType: 'number',
            initValue: paras.ZBID,
            fetch: {
              tableName: 'ShJhcwDcSetJYDW',
              optionTpl: 'ZBMC',
              valueTpl: 'ZBID',
              isDao: true,
              where: where,
            },
          },
        ],
      }}
    />
  );
};
export default Zb;
